//
//  DZSearchViewController.m
//  DaZhePrivateHomeCuisineClient
//
//  Created by zouzhisheng on 15-2-11.
//  Copyright (c) 2015年 zouzhisheng. All rights reserved.
//

#import "DZSearchViewController.h"
#import "DZRestautantModel.h"
#import "DZListViewController.h"
@interface DZSearchViewController (){
    NSMutableArray *dataArray;
}

@end

@implementation DZSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    dataArray=[NSMutableArray array];
    [self addBgView];
    [self.navView addSubview:self.searchField];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.searchField becomeFirstResponder];
}
-(void)addBgView{
    UIImageView *searchView = [[UIImageView alloc] initWithFrame:CGRectMake(70, SYSTEM_VERSION>=7.0?26:6, 352/2+40, 32)] ;
    UIImage *image=[UIImage imageNamed:@"img_mall_searchbar.png"];
    UIImage *afterImage=[image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 50, 5, 25)];
    [searchView setImage:afterImage];
    [searchView setBackgroundColor:[UIColor clearColor]];
    [self.navView addSubview:searchView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UITextField *)searchField{
    if (!_searchField) {
        _searchField=[[UITextField alloc] initWithFrame:CGRectMake(95,SYSTEM_VERSION>=7.0?28:8, 352/2+15, 32)];
        UIImageView *leftImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        leftImageView.image=[UIImage imageNamed:@"search"];
        _searchField.leftView=leftImageView;
        if (SYSTEM_VERSION >= 7) {
            _searchField.tintColor = [UIColor whiteColor];
        }
        _searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchField.returnKeyType = UIReturnKeySearch;
        _searchField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;// default is center
        _searchField.keyboardType = UIKeyboardTypeDefault;
        _searchField.textColor=[UIColor whiteColor];
        _searchField.borderStyle=UITextBorderStyleNone;
        _searchField.returnKeyType=UIReturnKeySearch;
        _searchField.delegate=self;
    }
    return _searchField;
}

-(void)backAction:(UIButton*)bt{
    [self.navigationController popViewControllerAnimated:NO];
}
-(void)searchData:(NSString *)keyword{
    NSDictionary *parmDir=[NSDictionary dictionaryWithObjectsAndKeys:@"search",@"act",keyword,@"keyword", nil];
    [[BaseService shareNetworkService] requestActivityParameters:parmDir DataSouce:self RequestType:Request_DaZheSearchZhekou Block:^(__weak id data, NSError *error) {
        if (!error) {
            DZRestautantModel *model=data;
            [dataArray removeAllObjects];
            for (int i=0; i<model.subjectInfo.count; i++) {
                [dataArray addObject:model.subjectInfo[i]];
            }
            DZListViewController *listVC=[[DZListViewController alloc] init];
            listVC.dataArray=dataArray;
            [self.navigationController pushViewController:listVC animated:YES];
        }
    }];
}
#pragma mark-UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text.length==0) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入搜索内容" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return YES;
    }
    [self searchData:textField.text];
    return YES;
}
- (UIView *)viewForLoadAnimationAtService:(RequestType)type{
    return self.view;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation]
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
